/**
 * Created by HUANGFR2 on 8/19/2016.
 */
const mongoose = require('mongoose');
const Stomp = require('stomp-client');
const config = require('../core/server/config/config');
var logger = require('../core/server/config/my_winston')

module.exports = {
    publishComplaint: function (oid, mid, cid, cb) {
        var client = new Stomp(config.jmsServer.url, config.jmsServer.port, '', '');
        client.connect(function (seesionId) {
            //console.log('JMS client connected!...');
            var message = {
                oId: oid.toString(),
                mId: mid.toString(),
                cId: cid.toString()
            };
            client.publish(config.jmsQueue.complaint_destination, JSON.stringify(message));
            var loggerMessage = message;
            loggerMessage.queue = config.jmsQueue.complaint_destination;
            console.log(loggerMessage);
            logger.log("info", loggerMessage);
            if (cb) {
                cb();
            }

            //client.disconnect(function () {
            //    console.log('jms '+config.jmsQueue.complaint_destination+' disconnect');
            //    if(cb){
            //        cb();
            //    }
            //})
        });
    },
    publicMerchant: function (mid, cb) {
        var client = new Stomp(config.jmsServer.url, config.jmsServer.port, '', '');
        client.connect(function (seesionId) {

            var message = {
                mId: mid.toString()
            };
            client.publish(config.jmsQueue.merchant_destination, JSON.stringify(message));
            var loggerMessage = message;
            loggerMessage.queue = config.jmsQueue.merchant_destination
            console.log(loggerMessage);
            logger.log("info", loggerMessage);
            if (cb) {
                cb();
            }
            //client.disconnect(function () {
            //    console.log('jms ' + config.jmsQueue.merchant_destination + ' disconnect');
            //    if(cb){
            //        cb();
            //    }
            //})
        });
    },
    publicRecommendedDish: function (mid, did, cb) {
        var client = new Stomp(config.jmsServer.url, config.jmsServer.port, '', '');
        client.connect(function (seesionId) {

            var message = {
                mId: mid.toString(),
                dId: did.toString()
            };
            client.publish(config.jmsQueue.dish_destination, JSON.stringify(message));
            var loggerMessage = message;
            loggerMessage.queue = config.jmsQueue.dish_destination;
            console.log(loggerMessage);
            logger.log("info", loggerMessage);
            if (cb) {
                cb();
            }
            //client.disconnect(function () {
            //    console.log('jms ' + config.jmsQueue.dish_destination + ' disconnect');
            //    if(cb){
            //        cb();
            //    }
            //})
        });
    },
    publishLowGrade: function (mid, cb) {
        var client = new Stomp(config.jmsServer.url, config.jmsServer.port, '', '');
        client.connect(function (seesionId) {

            var message = {
                mId: mid.toString()
            };
            client.publish(config.jmsQueue.lowGrade_destination, JSON.stringify(message));
            var loggerMessage = message;
            loggerMessage.queue = config.jmsQueue.lowGrade_destination
            console.log(loggerMessage);
            logger.log("info", loggerMessage);
            if (cb) {
                cb();
            }
            //client.disconnect(function () {
            //    console.log('jms ' + config.jmsQueue.lowGrade_destination + ' disconnect');
            //    if(cb){
            //        cb();
            //    }
            //})
        })
    }
};

